using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.UI;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Testability;
using NUnit.Framework;
using Firestarter.DB.Schema.Test.Logic;

namespace Firestarter.DB.Schema.Test.UI
{
    [TestFixture]
    public class TestFilteredTreeViewController : TestBase
    {
        
        private static string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }
        [Test]
        public void Test_ApplyFilter()
        {
            //---------------Set up test pack-------------------
            TreeViewWin treeView = new TreeViewWin();
            FilteredTreeViewController filteredTreeViewController = new FilteredTreeViewController(treeView);
            DBDatabase dbDatabase = new DBDatabase { DatabaseName = GetRandomString() };
            DBTable dbTable = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTable);
            DBTable dbTableInfo2 = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTableInfo2);
            DBTable dbTableInfo3 = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTableInfo3);
            filteredTreeViewController.LoadTreeView(dbDatabase, 2);
            //-------------Assert Preconditions -------------
            Assert.AreEqual(1, treeView.Nodes.Count);
            ITreeNode rootNode = treeView.Nodes[0];
            Assert.AreEqual(dbDatabase.ToString(), rootNode.Text);
            Assert.AreEqual(2, rootNode.Nodes.Count);
            ITreeNode relationshipNode = rootNode.Nodes[0];
            Assert.AreEqual("Tables", relationshipNode.Text);

            ITreeNode relationshipNodeViews = rootNode.Nodes[1];
            Assert.AreEqual("Views", relationshipNodeViews.Text);
            Assert.AreEqual(3, relationshipNode.Nodes.Count);
            //---------------Execute Test ----------------------
            filteredTreeViewController.ApplyFilter(bo => bo != dbTableInfo2);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, treeView.Nodes.Count);
            Assert.AreEqual(dbDatabase.ToString(), rootNode.Text);
            Assert.AreEqual(2, rootNode.Nodes.Count, "Views and Tables");
            Assert.AreEqual("Tables", relationshipNode.Text);
            Assert.AreEqual(2, relationshipNode.Nodes.Count);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTable), relationshipNode.Nodes[0]);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTableInfo3), relationshipNode.Nodes[1]);
        }

        [Test]
        public void Test_ApplyFilter_ChangeFilter()
        {
            //---------------Set up test pack-------------------
            TreeViewWin treeView = new TreeViewWin();
            FilteredTreeViewController filteredTreeViewController = new FilteredTreeViewController(treeView);
            DBDatabase dbDatabase = new DBDatabase{DatabaseName = GetRandomString() };
            DBTable dbTable = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTable);
            DBTable dbTableInfo2 = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTableInfo2);
            DBTable dbTableInfo3 = new DBTable(GetRandomString());
            dbDatabase.Tables.Add(dbTableInfo3);
            filteredTreeViewController.LoadTreeView(dbDatabase, 2);
            filteredTreeViewController.ApplyFilter(bo => bo != dbTable);
            //-------------Assert Preconditions -------------
            Assert.AreEqual(1, treeView.Nodes.Count);
            ITreeNode rootNode = treeView.Nodes[0];
            Assert.AreEqual(dbDatabase.ToString(), rootNode.Text);
            Assert.AreEqual(2, rootNode.Nodes.Count, "Should have Tables and Views");
            ITreeNode relationshipNode = rootNode.Nodes[0];
            Assert.AreEqual("Tables", relationshipNode.Text);
            Assert.AreEqual(2, relationshipNode.Nodes.Count);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTableInfo2), relationshipNode.Nodes[0]);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTableInfo3), relationshipNode.Nodes[1]);
            //---------------Execute Test ----------------------
            filteredTreeViewController.ApplyFilter(bo => bo != dbTableInfo2);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, treeView.Nodes.Count);
            Assert.AreEqual(dbDatabase.ToString(), rootNode.Text);
            Assert.AreEqual(2, rootNode.Nodes.Count, "Should have Tables and Views" );
            Assert.AreEqual("Tables", relationshipNode.Text);
            Assert.AreEqual(2, relationshipNode.Nodes.Count);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTable), relationshipNode.Nodes[0]);
            Assert.AreEqual(filteredTreeViewController.GetBusinessObjectTreeNode(dbTableInfo3), relationshipNode.Nodes[1]);
        }
    }
}
